package com.google.rui.utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class NumberUtil {
	public static String getNumberFormat(Object num) {
		String t = getNumberFormat(num, "0.00");
		return t;
	}

	public static double retentionDouble(double num, int digits) {
		try {
			NumberFormat formater = DecimalFormat.getInstance();
			formater.setMaximumFractionDigits(digits);
			formater.setMinimumFractionDigits(digits);
			return Double.parseDouble(formater.format(num));
		} catch (NumberFormatException e) {
			return num;
		}
	}
	
	public static double retentionDouble(double num) {
		return retentionDouble(num,2);
	}

	public static String getNumberFormat(Object num, String format) {
		try {
			String t = new DecimalFormat(format).format(num);
			return t;
		} catch (Exception e) {
			return "";
		}
	}

	public static String getNumberFormat(String num) {
		try {
			double d = Double.parseDouble(num);
			String t = getNumberFormat(d);
			return t;
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 转换 数据格式
	 * 
	 * @param num
	 *            数据
	 * @param format
	 *            转换格式
	 * @return
	 */
	public static String getNumberFormat(String num, String format) {
		try {
			double d = Double.parseDouble(num);
			String t = getNumberFormat(d, format);
			return t;
		} catch (Exception e) {
			return "";
		}

	}

}
